package org.goyo.main.domain.entity.system;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
/**
 * 系统用户
 * @author chenchuan
 */
@Entity
public class SysUser implements UserDetails,Serializable {
	
	private static final long serialVersionUID = 1L;
	
	@Id
	@GenericGenerator(name = "generator", strategy = "org.hibernate.id.UUIDGenerator")
	@GeneratedValue(generator = "generator")
	private String uuid;
	/**
	 * 用户名
	 */
	@Column
	private String userName;
	
	/**
	 * 密码
	 */
	@Column
	private String password;
	/**
	 * 用户权限集合
	 */
	@Transient
	private  Set<GrantedAuthority> authorities;
	/**
	 *  账户是否到期
	 */
	@Column
	private boolean isAccountNonExpired;
	/**
	 *  账户是锁定
	 */
	@Column
	private boolean isAccountNonLocked;
	/**
	 * 证书是否到期
	 */
	@Column
	private boolean isCredentialsNonExpired;
	/**
	 * 账户是否可用
	 */
	@Column
	private boolean isEnabled;
	/**
	 * 角色
	 */
	@Transient
	private Set<SysRole> sysroles;
	
	/**
	 * 超级用户
	 */
	@Column(updatable=false)  
	private boolean isSupper;
	
	/**
	 * 备注
	 */
	@Column
	private String remark;
	
	/**
	 * 真实姓名
	 */
	@Column
	private String trueName;
	/**
	 * 座机号
	 */
	@Column
	private String phone;
	/**
	 * 手机号
	 */
	@Column
	private String mobile;
	/**
	 * 邮箱
	 */
	@Column
	private String mail;
	
	/**
	 * 居住地
	 */
	@Column
	private String xjAddress;
	
	/**
	 * 身份证号
	 */
	@Column
	private String idCard;
	/**
	 * 身份证地址
	 */
	@Column
	private String idAddress;
	/**
	 * 所属部门
	 */
	@ManyToOne
	@JoinColumn(name="depGroupId")
	private SysDepGroup depGroup;
	
	/**
	 * 接收推送消息客户端id
	 */
	@Transient
	private String msgClientId;
	
	/**
	 * 职位
	 */
	@Column
	private String position;
	
	/**
	 * qq
	 */
	@Column
	private String qq;
	/**
	 * 创建日期
	 */
	@Column
	private Date createDate;
	
	public Set<SysRole> getSysroles() {
		return sysroles;
	}

	public void setSysroles(Set<SysRole> sysroles) {
		this.sysroles = sysroles;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setAuthorities(Set<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	public void setAccountNonExpired(boolean isAccountNonExpired) {
		this.isAccountNonExpired = isAccountNonExpired;
	}

	public void setAccountNonLocked(boolean isAccountNonLocked) {
		this.isAccountNonLocked = isAccountNonLocked;
	}

	public void setCredentialsNonExpired(boolean isCredentialsNonExpired) {
		this.isCredentialsNonExpired = isCredentialsNonExpired;
	}

	public void setEnabled(boolean isEnabled) {
		this.isEnabled = isEnabled;
	}

	public Collection<? extends GrantedAuthority> getAuthorities() {
		return this.authorities;
	}

	public String getPassword() {
		return this.password;
	}

	public boolean isAccountNonExpired() {
		return this.isAccountNonExpired;
	}

	public boolean isAccountNonLocked() {
		return this.isAccountNonLocked;
	}

	public boolean isCredentialsNonExpired() {
		return this.isCredentialsNonExpired;
	}

	public boolean isEnabled() {
		return this.isEnabled;
	}
	public String getUuid() {
		if("".equals(uuid))
			return null;
		return uuid;
	}
	public void setUuid(String uuid) {
		if("".equals(uuid))
			this.uuid = null;
		this.uuid = uuid;
	}
	
	@Override
	public boolean equals(Object user) {
		if(user == null || !(user instanceof SysUser))
			return false;
		SysUser sysuser = (SysUser)user;
		
		return (this.getUserName().equals(sysuser.getUserName()) && this.getPassword().equals(sysuser.getPassword()));
	}
	@Override
	public int hashCode() {
		return this.getUserName().hashCode();
	}

	public boolean isSupper() {
		return isSupper;
	}

	public void setSupper(boolean isSupper) {
		this.isSupper = isSupper;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getTrueName() {
		return trueName;
	}

	public void setTrueName(String trueName) {
		this.trueName = trueName;
	}

	@Override
	public String getUsername() {
		return this.userName;
	}
	
	public String setUsername(String userName){
		return this.userName = userName;
	}

	public String getUserName() {
		return userName;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getXjAddress() {
		return xjAddress;
	}

	public void setXjAddress(String xjAddress) {
		this.xjAddress = xjAddress;
	}

	public String getIdCard() {
		return idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}

	public String getIdAddress() {
		return idAddress;
	}

	public void setIdAddress(String idAddress) {
		this.idAddress = idAddress;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public SysDepGroup getDepGroup() {
		return depGroup;
	}

	public void setDepGroup(SysDepGroup depGroup) {
		if(depGroup != null &&("".equals(depGroup.getUuid()) || depGroup.getUuid()==null))
			this.depGroup = null;
		else
			this.depGroup = depGroup;	
	}

  public String getMsgClientId() {
    return msgClientId;
  }

  public void setMsgClientId(String msgClientId) {
    this.msgClientId = msgClientId;
  }

/*	public SysDepGroup getDepgroup() {
		return depGroup;
	}*/
  
  public static SysUser getSupplier(){
	  SysUser user = new SysUser();
	  user.setUserName("administrator");
	  user.setPassword("Usc3UzPf8coGhXYNa72syg==");
	  user.setSupper(true);
	  return user;
  }

	public String getPosition() {
		return position;
	}
	
	public void setPosition(String position) {
		this.position = position;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	
	
  
	
}
